package com.atguigu.sta.service.impl;

import com.atguigu.sta.client.UCenterClient;
import com.atguigu.sta.entity.StatisticsDaily;
import com.atguigu.sta.mapper.StatisticsDailyMapper;
import com.atguigu.sta.service.StatisticsDailyService;
import com.atguigu.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author ld
 * @since 2022-08-26
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Resource
    private UCenterClient uCenterClient;
    @Override
    public void getCountRegister(String day) {

        LambdaQueryWrapper<StatisticsDaily> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(StatisticsDaily::getDateCalculated,day);
        baseMapper.delete(queryWrapper);

        R countRegisterR = uCenterClient.getCountRegister(day);
        Integer countRegister = (Integer) countRegisterR.getData().get("countRegister");
        StatisticsDaily statisticsDaily=new StatisticsDaily();

        statisticsDaily.setDateCalculated(day);
        statisticsDaily.setRegisterNum(countRegister);
        statisticsDaily.setLoginNum(RandomUtils.nextInt(100,200));
        statisticsDaily.setVideoViewNum(RandomUtils.nextInt(100,200));
        statisticsDaily.setCourseNum(RandomUtils.nextInt(100,200));

        baseMapper.insert(statisticsDaily);
    }

    @Override
    public Map<String, Object> getData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> queryWrapper=new QueryWrapper<>();
        queryWrapper.between("date_calculated",begin,end)
                .select("date_calculated",type);
        List<StatisticsDaily> statisticsDailies = baseMapper.selectList(queryWrapper);
        List<String> date_calculatedList=new ArrayList<>();
        List<Integer> dataList=new ArrayList<>();
        for(StatisticsDaily statisticsDaily:statisticsDailies){
            date_calculatedList.add(statisticsDaily.getDateCalculated());
            switch(type){
                case "login_num":
                    dataList.add(statisticsDaily.getLoginNum());
                    break;
                case "register_num":
                    dataList.add(statisticsDaily.getRegisterNum());
                    break;
                case "video_view_num":
                    dataList.add(statisticsDaily.getVideoViewNum());
                    break;
                default:
                    dataList.add(statisticsDaily.getCourseNum());
                    break;
            }
        }
        Map<String,Object> map=new HashMap<>();
        map.put("date_calculatedList",date_calculatedList);
        map.put("dataList",dataList);
        return map;
    }
}
